package LC;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName L95
 * @Description: TODO
 * @Author hl
 * @Date 2020/8/3
 * @Version V1.0
 **/
public class L93 {

    public static void main(String[] args) {


        List<String> list=restoreIpAddresses("25525511135");
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }


    }
    public static List<String> restoreIpAddresses(String s) {
    List<String> result=new ArrayList<>();
    StringBuilder temp=new StringBuilder();
    int k=0;
    seach(k,s,result,temp);
    return  result;
    }
    public static void seach(int k,String s,List<String> result,StringBuilder temp){
        if(k==4||s.length()==0){
            if(k==4&&s.length()==0){
                result.add(temp.toString());
            }

            return;
        }
        for(int i=0;i<s.length()&&i<=2;i++){//这里要先小于s.length（）防止边界溢出，小于等于2是因为最大为255
            if(i!=0&&s.charAt(0)=='0'){//防止出现00  01 这种以0开头的数字
                break;
            }
            String part=s.substring(0,i+1);
            if(Integer.valueOf(part)<=255){
                if(temp.length() !=0){
                    part="."+part;
                }
                temp.append(part);
                seach(k+1,s.substring(i+1),result,temp);
                temp.delete(temp.length()-part.length(),temp.length());
            }


    }


    }



}
